package com.qk.controller;

import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping
    public Result list(UserDto userDto){
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
    @PostMapping
    public Result add(@RequestBody User user){
        log.info("添加用户：{}",user);
        userService.add(user);
        return Result.success();
    }
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("删除用户：{}",ids);
        userService.delete(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        User find= userService.findById(id);
        return Result.success(find);
    }
    @PutMapping
    public Result updateUser(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }
    @GetMapping("/role/{roleLabel}")
    public Result findByRoleLacel(@PathVariable String roleLabel){
        List<User> findList= userService.findUser(roleLabel);
        return Result.success(findList);
    }
    @GetMapping("/dept/{deptId}")
    public Result findByDeptId(@PathVariable Integer deptId){
        List<User> userList= userService.findByDeptId(deptId);
        return Result.success(userList);
    }
    @GetMapping("/list")
    public Result selectAll(){
        List<User> userList= userService.selectAll();
        return Result.success(userList);
    }
}
